#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "mhlabel.h"
#include "com.h"

/*
 * set style of label color
 */
void SetLabelColorStyle(HWND hWnd, int nStyle)
{
    SetWindowAdditionalData(hWnd, nStyle);
}
/*
 *
 */
int GetLabelColorStyle(HWND hWnd, int nStyle)
{
    return GetWindowAdditionalData(hWnd);
}
/*
 *
 */
static int MhLabelProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case MSG_CREATE:
        {
        }
        break;
    case MSG_PAINT:
        {
        	RECT rect;
        	GetClientRect(hWnd, &rect);
        	HDC hdc = BeginPaint(hWnd);
        	SetBkMode(hdc, BM_TRANSPARENT);
        	DWORD nStyle = GetWindowStyle(hWnd);
        	uint nFormat = DT_VCENTER | DT_SINGLELINE;
        	if(nStyle & BS_LEFT)
        	{
        	    nFormat |= DT_LEFT;
        	}
        	else if(nStyle & BS_RIGHT)
        	{
        	    nFormat |= DT_RIGHT;
        	}
        	else
        	{
        	    nFormat |= DT_CENTER;
        	}
        	const char *sc = GetWindowCaption (hWnd);
        	int nData = GetWindowAdditionalData(hWnd);
        	if(nData & MH_LABEL_COLOR_1GROUP)
        	{
        	    SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_3GROUP)
        	{
        	    SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_4GROUP)
        	{
        	    SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_5GROUP)
        	{
        	    SetTextColor(hdc, COLOR_HQ_WHITE(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_6GROUP)
        	{
        	    SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_7GROUP)
        	{
        	    SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
        	}
        	else if(nData & MH_LABEL_TEXT_GREEN)
        	{
        	    SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
        	}
        	else if(nData & MH_LABEL_TEXT_WHITE)
        	{
        	    SetTextColor(hdc, COLOR_HQ_WHITE(hdc));
        	}
        	else if(nData & MH_LABEL_TEXT_CYAN)
        	{
        	    SetTextColor(hdc, COLOR_HQ_CYAN(hdc));
        	}
        	else if(nData & MH_LABEL_TEXT_YELLOW)
            {
                SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            }
        	else if(nData & MH_LABEL_TEXT_RED)
        	{
        	    SetTextColor(hdc, COLOR_HQ_RED(hdc));
        	}
        	else if(nData & MH_LABEL_BOOL_TYPE)
        	{
        	    int n = 0;
        	    if(sc)
        	    {
        	        n = atoi(sc);
        	    }
        	    SetTextColor(hdc, n ? COLOR_HQ_RED(hdc) : COLOR_HQ_CYAN(hdc));
        	}
        	DrawText(hdc, sc, -1, &rect, nFormat);
            EndPaint(hWnd, hdc);
        }
        break;
    case MSG_ERASEBKGND:
        {
            int nData = GetWindowAdditionalData(hWnd);
        	HDC hdc = GetClientDC(hWnd);
        	RECT rect;
        	if(lParam)
        	{
        	    memcpy(&rect, (RECT*)lParam, sizeof(RECT));
        	}
        	else
        	{
        	    GetClientRect(hWnd, &rect);
        	}
        	int nWidth = GET_RECT_WIDTH(rect);
        	int nHeight = GET_RECT_HEIGHT(rect);
        	if(nData & MH_LABEL_COLOR_1GROUP)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BLACK(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_3GROUP)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BLACK(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_4GROUP)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_DUSTY(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_5GROUP)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BLUE(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_6GROUP)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BLUE(hdc));
        	}
        	else if(nData & MH_LABEL_COLOR_7GROUP)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BLUE(hdc));
        	}
        	else if(nData & MH_LABEL_BOOL_TYPE)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BLACK(hdc));
        	}
        	else if(nData & MH_LABEL_BKGND_2GREEN)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_2GREEN(hdc));
        	}
        	else if(nData & MH_LABEL_BKGND_3GREEN)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_3GREEN(hdc));
        	}
        	else if(nData & MH_LABEL_BKGND_BLACK)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BLACK(hdc));
        	}
        	else if(nData & MH_LABEL_BKGND_BLUE)
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BLUE(hdc));
        	}
        	else
        	{
        	    SetBrushColor (hdc, COLOR_HQ_BKGND(hdc));
        	}
        	FillBox (hdc, rect.left, rect.top, nWidth, nHeight);
        	ReleaseDC(hdc);
        }
        return 0;
    }

    return DefaultControlProc (hWnd, message, wParam, lParam);
}
/*
 *
 */
BOOL RegisterMhLabel (void)
{
    WNDCLASS MyClass;

    MyClass.spClassName = MH_LABEL_NAME;
    MyClass.dwStyle     = WS_NONE;
    MyClass.dwExStyle   = WS_EX_NONE;
    MyClass.hCursor     = GetSystemCursor (IDC_ARROW);
    MyClass.iBkColor    = COLOR_lightwhite;
    MyClass.WinProc     = MhLabelProc;
    return RegisterWindowClass (&MyClass);
}
/*
 *
 */
void UnregisterMhLabel (void)
{
    UnregisterWindowClass (MH_LABEL_NAME);
}


